package com.portal.flowable.config;

import com.portal.flowable.listener.BpmProcessInstanceEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.engine.RuntimeService;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.Resource;

/**
 * 全局监听配置 ContextRefreshedEvent在类被初始化之后触发
 */
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Resource
    private SpringProcessEngineConfiguration configuration;
    @Resource
    private BpmProcessInstanceEventListener bpmProcessInstanceEventListener;
    @Resource
    private RuntimeService runtimeService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //任务完成全局监听
        //dispatcher.addEventListener(activityCompleteListener, FlowableEngineEventType.TASK_CREATED);
//        runtimeService.addEventListener(processInstanceCompleteListener, FlowableEngineEventType.PROCESS_COMPLETED);
        runtimeService.addEventListener(bpmProcessInstanceEventListener);
    }
}


